<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>投票</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <style>
        .layui-hide{  /*最后的pic为字段的field*/
            height: 100%;
            max-width: 100%;
        }
        .layui-table-cell{
            height:auto !important;
        }
    </style>
</head>
<body>
<div id="aaa"></div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">投票管理</div>
                <div class="layui-card-body">
                    <div class="layui-form-item layui-form">

                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" id="title" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">审核状态</label>
                            <div class="layui-input-block">
                                <select name="is_pass" lay-filter="aihao" id="is_pass">
                                    <option value=""></option>
                                    <option value="0">审核中</option>
                                    <option value="1">通过</option>
                                    <option value="2">不通过</option>
                                </select>
                            </div>
                        </div>
                         <div class="layui-inline">
                            <label class="layui-form-label">审核时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="test6" placeholder="  ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-btn-group test-table-operate-btn">
                                <button class="layui-btn" data-type="searchUserButton">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate">
                        <script type="text/html" id="is_use_switch">
                            <input type="checkbox" name="is_display" lay-skin="switch" lay-text="上架|下架" lay-filter="is_use_switch" value="{{ d.is_display }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_display == 1 ? 'checked' : '' }}>
                        </script>
                    </table>
                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-normal layui-btn-xs"  lay-event="option">投票选项</a>
                        {{# if(d.is_pass==0){ }}
                        <a class="layui-btn layui-bg-green layui-btn-xs" lay-event="yesPass">审核</a>
                        {{# }else if(d.is_pass==1){ }}
                        <a class=" layui-btn layui-btn-disabled layui-btn-xs">通过</a>
                        {{# } else if(d.is_pass==2){ }}
                        <a class="layui-btn layui-btn-disabled layui-btn-xs">不通过</a>
                        {{# } }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs"  lay-event="live_action">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="contentShow" style="display:none;padding: 30px;">
    <p id="consultation"></p>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
        layui.use('laydate', function(){
        var laydate = layui.laydate;
        //日期范围
        laydate.render({
            elem: '#test6'
            ,range: true
        });
    });
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Vote/vote')}"
            ,height: 'full-155'
            ,cols: [[
                {field:'id',align:'center', title: 'ID'}
                ,{field:'nickname',align:'center',title: '投票发起人'}
                ,{field:'title',align:'center',title: '标题'}
                ,{field:'image',width:120,align:'center',title: '投票图片',style:"height:100px;",templet:function(d){
                        return "<img src=" +d.image+ ">"}}
                ,{field:'content',align:'center',title: '投票说明'}
                ,{field:'end_time', align:'center', title: '投票截止时间'}
                ,{field:'create_time', align:'center', title: '申请时间'}
                ,{field:'pass_time', align:'center', title: '审核时间'}
                ,{field:'is_display',width:150, align:'center', title: '是否上架',templet: '#is_use_switch'}
                ,{align:'center',width:350, title: '操作', toolbar: '#test-table-operate-barDemo'}
            ]]
            ,page:true
            ,limit:20
        });

        form.render(null,'component-form-group');

        laydate.render({
            elem: '#search_time'
        });
        //监听是否上架
        form.on('switch(is_use_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var is_display = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Vote/voteIsShow')}",
                type: 'post',
                dataType: 'json',
                data: {id:json.id,is_display:is_display},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg('修改失败',{icon:2,time:1000},function(){
                            window.location.reload();
                        });
                    }
                }
            })
        });
        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'yesPass'){
                    layer.confirm('投票申请',{
                        btn: ['通过', '不通过']
                        ,yes:function(){
                            var loading = layer.load(1, {
                                shade: [0.1,'#fff'] //0.1透明度的白色背景
                            });
                            $.ajax({
                                url: "{:url('Vote/voteIsPass')}",
                                type: 'post',
                                dataType: 'json',
                                data: {id:data.id,uid:data.uid,is_pass:1,title:data.title},
                                success: function(msg){
                                    layer.close(loading);
                                    if (msg === 0) {
                                        layer.msg('已通过', {icon: 1, time: 1000},function(){
                                            window.location.reload();
                                        });
                                    }else{
                                        layer.msg('系统错误',{icon:2,time:1000});
                                    }
                                }
                            });
                        }
                        ,btn2:function(){
                            layer.open({
                                id:1,
                                type: 1,
                                title:'请输入拒绝原因',
                                // style: 'width:500px;height:auto;',
                                content: "<div style='display:flex;justify-content:center;'><textarea id='area' style='width:400px;height:135px;'></textarea></div>",
                                btn:['保存','取消'],
                                yes:function (index,layero) {

                                    //获取输入框里面的值
                                    var closeContent =  $("#area").val();

                                    if(closeContent == ''){
                                        layer.msg('请输入拒绝原因',{icon:2,time:1000});
                                        return;
                                    }
                                    //layer.close(index);
                                    // 在这里提交数据
                                    var loading = layer.load(1, {shade: [0.1,'#fff'] //0.1透明度的白色背景
                                    });
                                    $.ajax({
                                        url: "{:url('Vote/voteIsPass')}",
                                        type: 'post',
                                        dataType: 'json',
                                        data: {id: data.id,uid:data.uid, is_pass:2,reject_reasons:closeContent,title:data.title},
                                        success: function (msg) {
                                            layer.close(loading);
                                            if (msg === 0) {
                                                layer.msg('未通过', {icon: 1, time: 1000},function(){
                                                    window.location.reload();
                                                });
                                            } else {
                                                layer.close(loading);
                                                layer.msg('系统错误', {icon: 2, time: 1000});
                                            }
                                        }
                                    })
                                },
                            });

                        }
                        ,btn3:function(){
                            layer.closeAll();
                        }
                }, function(){layer.closeAll();
                });
            }else if(obj.event === 'option'){
                top.layui.index.openTabsPage("{:url('Vote/voteOption')}?id=" + data.id,'查看投票选项');
            }else if(obj.event === 'live_action'){
                layer.confirm('确认删除'+data.title+'吗？', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Vote/vote_del')}",
                        type: 'post',
                        dataType: 'json',
                        data: {id:data.id},
                        success: function(msg){
                            layer.close(loading);
                            if (msg === "1") {
                                obj.del();
                                layer.close(index);
                            }else{
                                layer.msg('删除失败',{icon:2,time:1000});
                            }
                        }
                    })

                });
            }

        });

        var $ = layui.$, active = {
            searchUserButton:function(){
                var title  = $('#title').val();
                var is_pass = $('#is_pass').val();
                var pass_time = $('#test6').val();
                table.reload('test-table-operate', {
                    where: {
                        title:title,
                        is_pass:is_pass,
                        pass_time:pass_time
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                //return false;
            }
        };
        //绑定点击事件
        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>


</body>
</html>